<template>
  <div class="theme-switcher" ref="dropdownRef">
    <button 
      class="theme-toggle-btn"
      @click="toggleDropdown"
      title="切换主题"
    >
      🎨
    </button>
    
    <div v-if="isOpen" class="theme-dropdown">
      <div class="theme-dropdown-header">选择主题</div>
      <div class="theme-list">
        <div
          v-for="theme in themes"
          :key="theme.id"
          class="theme-option"
          :class="{ active: currentTheme === theme.id }"
          @click="handleThemeChange(theme.id)"
        >
          <div class="theme-info">
            <span 
              class="theme-color-preview" 
              :style="{ backgroundColor: theme.color }"
            />
            <div class="theme-text">
              <span class="theme-name">{{ theme.name }}</span>
              <span class="theme-description">{{ theme.description }}</span>
            </div>
          </div>
          <span v-if="currentTheme === theme.id" class="theme-check">✓</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted, watch } from 'vue'
import { dropdownManager } from '../utils/dropdownManager'

const DROPDOWN_ID = 'theme-switcher'
const currentTheme = ref('default')
const isOpen = ref(false)
const dropdownRef = ref<HTMLElement | null>(null)
let autoCloseTimer: number | null = null
let unsubscribe: (() => void) | null = null

const themes = [
  { id: 'default', name: '深蓝专业', color: '#1e40af', description: 'Professional Blue' },
  { id: 'emerald', name: '翡翠绿彩', color: '#059669', description: 'Emerald Green' },
  { id: 'purple', name: '深夜紫色', color: '#7c3aed', description: 'Midnight Purple' },
  { id: 'dark', name: '黑色系', color: '#1f2937', description: 'Dark Theme' },
]

const applyTheme = (themeId: string) => {
  const root = document.documentElement
  if (themeId === 'default') {
    root.removeAttribute('data-theme')
  } else {
    root.setAttribute('data-theme', themeId)
  }
}

const handleThemeChange = (themeId: string) => {
  currentTheme.value = themeId
  applyTheme(themeId)
  localStorage.setItem('theme', themeId)
  isOpen.value = false
}

const toggleDropdown = () => {
  isOpen.value = !isOpen.value
  if (isOpen.value) {
    dropdownManager.notifyOpen(DROPDOWN_ID)
  }
}

const handleMouseEnter = () => {
  if (autoCloseTimer) {
    clearTimeout(autoCloseTimer)
    autoCloseTimer = null
  }
}

const handleMouseLeave = () => {
  autoCloseTimer = setTimeout(() => {
    isOpen.value = false
  }, 2000)
}

watch(isOpen, (newValue) => {
  const dropdownElement = dropdownRef.value
  if (dropdownElement && newValue) {
    dropdownElement.addEventListener('mouseenter', handleMouseEnter)
    dropdownElement.addEventListener('mouseleave', handleMouseLeave)
    
    autoCloseTimer = setTimeout(() => {
      isOpen.value = false
    }, 2000)
  } else if (dropdownElement) {
    dropdownElement.removeEventListener('mouseenter', handleMouseEnter)
    dropdownElement.removeEventListener('mouseleave', handleMouseLeave)
    if (autoCloseTimer) {
      clearTimeout(autoCloseTimer)
    }
  }
})

onMounted(() => {
  const savedTheme = localStorage.getItem('theme') || 'default'
  currentTheme.value = savedTheme
  applyTheme(savedTheme)
  
  unsubscribe = dropdownManager.subscribe((openedId) => {
    if (openedId !== DROPDOWN_ID) {
      isOpen.value = false
    }
  })
})

onUnmounted(() => {
  if (unsubscribe) {
    unsubscribe()
  }
  if (autoCloseTimer) {
    clearTimeout(autoCloseTimer)
  }
})
</script>

<style scoped>
@import './ThemeSwitcher.css';
</style>